/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import java.awt.image.BufferedImage;
import net.worlds.AssetLoader;
import net.worlds.AssetManager;
import net.worlds.WorldsException;
import net.worlds.PropChangeHandler;
import org.jbox2d.common.Vec2D;


public class Texture extends GameObject{
    private BufferedImage image;
    public int screenWidth;
    public int screenHeight;
    private Part parent;
    private void rebuildTexture() throws WorldsException
    {
        if(image == null)
            return;
        if(parent != null)
            parent.setTexture(null);
        Part p = ((Part)getParent());
        parent = p;
        parent.showSelf = getBooleanProp("ShowPart");
        Vec2D s = null;
        if(p instanceof Box || p instanceof Water || p instanceof Ladder)
            s = p.getVec2DProp("Size").mul(8);
        else if(p instanceof Circle)
            s = new Vec2D((float) p.getFloatProp("Radius") * 2, (float) p.getFloatProp("Radius") * 2).mul(8);
        p.setTexture(AssetManager.tileTexture(image, (int) s.x, (int) s.y, getBooleanProp("TileX"), getBooleanProp("TileY"), getBooleanProp("FlipX"), getBooleanProp("FlipY")));
    }
    public Texture() throws WorldsException
    {
        super("Texture");
        addPropChangeHandler("Image", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                final String path = value.toString();
                Runnable r = new Runnable()
                {

                    public void run() {
                        try
                        {
                            image = AssetManager.loadImage(path, path);
                            if(inWorld && getParent() instanceof Part)
                                rebuildTexture();
                        } catch (Exception ex) {
                            System.err.println("Invalid texture URL");
                        }
                    }
                };
                AssetLoader.getInstance().addItem(r);
            }
        });
        addPropChangeHandler("TileX", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(inWorld && parent instanceof Part)
                    rebuildTexture();
            }
        });
        addPropChangeHandler("TileY", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(inWorld && parent instanceof Part)
                    rebuildTexture();
            }
        });
        addPropChangeHandler("FlipX", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(inWorld && parent instanceof Part)
                    rebuildTexture();
            }
        });
        addPropChangeHandler("FlipY", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(inWorld && parent instanceof Part)
                    rebuildTexture();
            }
        });
        addPropChangeHandler("Parent", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                onParentChanged((GameObject) oldValue, (GameObject) value);
                if(inWorld)
                {
                    if(value instanceof Part && value != null)
                        rebuildTexture();
                    else if(parent != null)
                        parent.setTexture(null);
                }
            }
        });
        addPropChangeHandler("ShowPart", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(parent != null)
                    parent.showSelf = (Boolean) value;
            }
        });
        //x_;
    }
    @Override
    public void destructor() throws WorldsException
    {
        super.destructor();
        if(parent != null)
        {
            parent.setTexture(null);
        }
    }
    @Override
    public void addToWorld() throws WorldsException
    {
        super.addToWorld();
        rebuildTexture();
    }
}
